#include<GL/glut.h>
#include<math.h>
#define MUNDO 4.0f
#define PI 3.141592654f

void dibujaCasa (float dx, float dy);
void dibujaCirculo(GLenum primitiva, float radio, float cx, float cy);
void dibujaCasa(float dx, float dy);
void dibuja (void);
void redimensiona (int ancho, int alto);

int main(int argc, char **argv) {
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Dibujando Casita");

    //Registro de funciones de callback
    glutDisplayFunc(dibuja);
    glutReshapeFunc(redimensiona);
    glutMainLoop();

    return 0;
}

void dibujaCasa(float dx, float dy) {
	//Cuerpo casa
	
	glColor3f(253.0/255.0, 255.0/255.0, 129.0/255.0);
	glBegin(GL_QUADS);
    	glVertex3f(-2.5, -3.5, 0.0);
    	glVertex3f(2.5, -3.5, 0.0);
    	glVertex3f(2.5, 0.0, 0.0);
    	glVertex3f(-2.5, 0.0, 0.0);
    glEnd();
    
    //Techo casa
    glColor3f(229.0 / 255.0, 138.0 / 255.0, 36.0 / 255.0);
    glBegin(GL_TRIANGLES);
    	glVertex3f(-3.0, 0.0, 0.0);
    	glVertex3f(3.0, 0.0, 0.0);
    	glVertex3f(0.0, 3.0, 0.0);
    glEnd();
    
    //Ventanas
    glColor3f(138.0/255.0, 251.0/255.0, 249.0/255.0);
    glBegin(GL_QUADS);
    	glVertex3f(-2.0, -1.5, 0.0);
    	glVertex3f(-1.0, -1.5, 0.0);
    	glVertex3f(-1.0, -0.5, 0.0);
    	glVertex3f(-2.0, -0.5, 0.0);
    	
    	glVertex3f(0.0, -1.5, 0.0);
    	glVertex3f(2.0, -1.5, 0.0);
    	glVertex3f(2.0, -0.5, 0.0);
    	glVertex3f(0.0, -0.5, 0.0);
    glEnd();
    dibujaCirculo(GL_POLYGON, 0.5f, 0.0f, 1.5);
    
    //Puerta
    glColor3f(118.0/255.0, 57.0/255.0, 20.0/255.0);
    glBegin(GL_QUADS);
    	glVertex3f(-0.5, -3.5, 0.0);
    	glVertex3f(0.5, -3.5, 0.0);
    	glVertex3f(0.5, -2.0, 0.0);
    	glVertex3f(-0.5, -2.0, 0.0);
    glEnd();
    
    //Chimenea
    glColor3f(240.0/255.0, 69.0/255.0, 2.0/255.0);
    glBegin(GL_POLYGON);
    	glVertex3f(1.0, 2.0, 0.0);
    	glVertex3f(2.0, 1.0, 0.0);
    	glVertex3f(2.0, 3.0, 0.0);
    	glVertex3f(1.0, 3.0, 0.0);
    glEnd();
}

void dibujaCirculo(GLenum primitiva, float radio, float cx, float cy) {
	float angulo;
	int i;
	glBegin(primitiva);
		for (i = 0; i < 1000; i++) {
			angulo = 2.0f * PI / 1000.0f * (float)i;
			glVertex3f(radio * cos(angulo) + cx, radio * sin(angulo) + cy, 0.0f);
		}
	glEnd();
}

void dibuja (void) {
	glClearColor(54.0/255.0, 230.0/255.0, 12.0/255.0, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    
    dibujaCasa(0, 0);
        
    glFlush();
}

void redimensiona (int ancho, int alto) {
	
   float aspectRadio;

    
	if (alto == 0) {
		alto = 1;
	}
	
	glViewport(0, 0, ancho, alto);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	aspectRadio = (float) ancho / (float) alto;
   
   if (ancho <= alto) {
		glOrtho(-MUNDO, MUNDO, -MUNDO / aspectRadio, MUNDO / aspectRadio, -MUNDO, MUNDO);
   } else {
		glOrtho(-MUNDO * aspectRadio, MUNDO * aspectRadio, -MUNDO, MUNDO, -MUNDO, MUNDO);
   }
   
	
}

/*void redimensiona (int ancho, int alto) {
	
	glViewport(0, 0, ancho, alto);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	   
	glOrtho(-MUNDO, MUNDO, -MUNDO, MUNDO, -MUNDO, MUNDO);

}*/

/*void redimensiona (int ancho, int alto) {
	
	glViewport(0, 0, ancho, alto);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	float spaceX = (float) ancho * MUNDO / 500.0;
	float spaceY = (float) alto * MUNDO / 500.0;
	   
	glOrtho(-spaceX, spaceX, -spaceY, spaceY, -MUNDO, MUNDO);

}*/
